//
//  WSQRangeSlider.m
//  RACTest
//
//  Created by 翁胜琼 on 2018/10/19.
//  Copyright © 2018 翁胜琼. All rights reserved.
//

#import "WSQRangeSlider.h"
#import <Masonry/Masonry.h>


@interface WSQRangeSlider()

@property (nonatomic,strong) UIImageView *leftImageView;
@property (nonatomic,strong) UIImageView *rightImageView;
@property (strong,nonatomic) UIView *progressView;
@property (strong,nonatomic) CALayer *backgroundLineLayer;  // 背景线段

@end

@implementation WSQRangeSlider


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setupDefaultValue];
        [self setupUI];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupDefaultValue];
    [self setupUI];
    
    
}

// setter
- (void)setCurrentMaxValue:(CGFloat)currentMaxValue
{
    _currentMaxValue = currentMaxValue;
    
    CGFloat rightPaddingPrecent = (_maxValue - currentMaxValue) / (_maxValue - _minValue);
    CGFloat rightPadding = rightPaddingPrecent * self.bounds.size.width;
    
    [self.rightImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-rightPadding);
    }];
    
}

- (void)setCurrentMinValue:(CGFloat)currentMinValue
{
    _currentMinValue = currentMinValue;
    
    CGFloat leftPaddingPrecent = currentMinValue / (_maxValue - _minValue);
    CGFloat leftPadding = self.bounds.size.width * leftPaddingPrecent;
    
    [self.leftImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(leftPadding);
    }];
}

- (void)setSliderWidth:(CGFloat)sliderWidth
{
    _sliderWidth = sliderWidth;
    
    [self.rightImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(sliderWidth));
    }];
    
    [self.leftImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(sliderWidth));
    }];
}

- (void)setSliderHeight:(CGFloat)sliderHeight
{
    _sliderHeight = sliderHeight;
    
    [self.rightImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(sliderHeight));
    }];
    
    [self.leftImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(sliderHeight));
    }];
}

- (void)setBackbroundProgressColor:(UIColor *)backbroundProgressColor
{
    _backbroundProgressColor = backbroundProgressColor;
    
    self.backgroundLineLayer.backgroundColor = backbroundProgressColor.CGColor;
}

- (void)setProgressViewHeight:(CGFloat)progressViewHeight
{
    _progressViewHeight = progressViewHeight;
    
    [self.progressView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(progressViewHeight));
    }];
    
    self.backgroundLineLayer.frame = CGRectMake(self.sliderWidth/2, (self.bounds.size.height - _progressViewHeight)/2, self.bounds.size.width - self.sliderWidth, _progressViewHeight);
}

- (void)setProgressColor:(UIColor *)progressColor
{
    _progressColor = progressColor;
    
    self.progressView.backgroundColor = progressColor;
}

- (void)setSliderImageName:(NSString *)sliderImageName
{
    _sliderImageName = [sliderImageName copy];
    
    UIImage *image = [UIImage imageNamed:_sliderImageName];
    self.leftImageView.image = image;
    self.rightImageView.image = image;
}

- (void)setLeftSliderImageName:(NSString *)leftSliderImageName
{
    _leftSliderImageName = [leftSliderImageName copy];
    
    self.leftImageView.image = [UIImage imageNamed:_leftSliderImageName];
}

- (void)setRightSliderImageName:(NSString *)rightSliderImageName
{
    _rightSliderImageName = [rightSliderImageName copy];
    
    self.rightImageView.image = [UIImage imageNamed:_rightSliderImageName];
}

// MARK: - setupUI
- (void)setupDefaultValue
{
    _sliderWidth = _sliderWidth != 0 ? _sliderWidth : 32;
    _sliderHeight = _sliderHeight != 0 ? _sliderHeight : 32;
    _progressViewHeight = _progressViewHeight != 0 ? _progressViewHeight : 5;
    _backbroundProgressColor = _backbroundProgressColor != nil ? _backbroundProgressColor : [UIColor lightGrayColor];
    _progressColor = _progressColor != nil ? _progressColor : [UIColor orangeColor];
    _sliderImageName = @"seek_ico_circle";
    _minValue = _minValue != 0 ? _minValue : 0;
    _currentMinValue = _currentMinValue != 0 ? _currentMinValue : _minValue;
    _maxValue = _maxValue != 0 ? _maxValue : 100;
    _currentMaxValue = _currentMaxValue != 0 ? _currentMaxValue : _maxValue;
}

- (void)setupUI
{
    [self setupImageView];
    [self setupProgressView];
}

- (void)setupProgressView
{
    CALayer *backgroundLineLayer = [CALayer layer];
    backgroundLineLayer.backgroundColor = _backbroundProgressColor.CGColor;
    backgroundLineLayer.frame = CGRectMake(self.sliderWidth/2, (self.bounds.size.height - _progressViewHeight)/2, self.bounds.size.width - self.sliderWidth, _progressViewHeight);
    self.backgroundLineLayer = backgroundLineLayer;
    [self.layer insertSublayer:backgroundLineLayer atIndex:0];
    
    UIView *progressView = [[UIView alloc] initWithFrame:CGRectMake(self.sliderWidth/2, (self.bounds.size.height - _progressViewHeight)/2, self.bounds.size.width- self.sliderWidth, _progressViewHeight)];
    progressView.backgroundColor = _progressColor;
    self.progressView = progressView;
    [self insertSubview:progressView atIndex:1];
    
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.leftImageView.mas_centerX);
        make.right.equalTo(self.rightImageView.mas_centerX);
        make.height.equalTo(@(self->_progressViewHeight));
    }];
}

- (void)setupImageView
{
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _sliderWidth, _sliderHeight)];
    leftImageView.image = [UIImage imageNamed:_sliderImageName];
    leftImageView.userInteractionEnabled = true;
    
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _sliderWidth, _sliderHeight)];
    rightImageView.userInteractionEnabled = true;
    rightImageView.image = [UIImage imageNamed:_sliderImageName];
    
    self.leftImageView = leftImageView;
    self.rightImageView = rightImageView;
    
    [self addSubview:leftImageView];
    [self addSubview:rightImageView];
    
    [leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.equalTo(self);
        make.width.equalTo(@(self->_sliderWidth));
        make.height.equalTo(@(self->_sliderHeight));
    }];
    
    [rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.centerY.equalTo(self);
        make.width.equalTo(@(self->_sliderWidth));
        make.height.equalTo(@(self->_sliderHeight));
    }];
    
    UIPanGestureRecognizer *leftPanGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(leftImageViewPanGesAction:)];
    [leftImageView addGestureRecognizer:leftPanGes];
    
    UIPanGestureRecognizer *rightPanGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rightImageViewPanGesAction:)];
    [rightImageView addGestureRecognizer:rightPanGes];
}

// 右滑块滑动
- (void)rightImageViewPanGesAction:(UIPanGestureRecognizer *)panGes
{
    CGPoint offset = [panGes translationInView:self.rightImageView];
    CGFloat rightPadding = self.bounds.size.width - CGRectGetMaxX(self.rightImageView.frame);
    
    rightPadding -= offset.x;
    
    // 避免超出右边界
    if (rightPadding < 0)
    {
        rightPadding = 0;
    }else if (CGRectGetMaxX(self.leftImageView.frame) >= (self.rightImageView.frame.origin.x + offset.x))
    {
        rightPadding += offset.x;
    }
    
    //    [self.rightImageView mas_updateConstraints:^(MASConstraintMaker *make) {
    //        make.right.equalTo(self).offset(-rightPadding);
    //    }];
    
    [panGes setTranslation:CGPointZero inView:self.rightImageView];
    
    // 计算 currentMaxValue
    self.currentMaxValue =  (self.bounds.size.width - rightPadding)/self.bounds.size.width*(_maxValue - _minValue) + _minValue;
}


// 左滑块滑动
- (void)leftImageViewPanGesAction:(UIPanGestureRecognizer *)panGes
{
    
    CGPoint offset = [panGes translationInView:self.leftImageView];
    CGRect origionalFrame = self.leftImageView.frame;
    
    // 即将要滑动到的位置
    origionalFrame.origin.x += offset.x;
    
    // 避免滑块滑出左边界
    if (origionalFrame.origin.x < 0)
    {
        origionalFrame.origin.x = 0;
    }else if (CGRectGetMaxX(origionalFrame) >= self.rightImageView.frame.origin.x)  // 避免左滑块超过右滑块
    {
        origionalFrame.origin.x -= offset.x;
    }
    
    //    [self.leftImageView mas_updateConstraints:^(MASConstraintMaker *make) {
    //            make.left.equalTo(self).offset(origionalFrame.origin.x);
    //    }];
    [panGes setTranslation:CGPointZero inView:self.leftImageView];
    
    // 计算 currentMinh 值
    self.currentMinValue = origionalFrame.origin.x / self.bounds.size.width * (_maxValue - _minValue) + _minValue;
    NSLog(@"_currentMinValue%f",_currentMinValue);
}
@end
